/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
/*
 * LoadTask.java
 *
 * Created on December 3, 2002, 2:26 PM
 */

package com.webdeninteractive.bie.qa.load;

import com.webdeninteractive.toolkit.threadpool.*;
import com.webdeninteractive.xbotts.client.api.client.*;
import com.webdeninteractive.xbotts.client.api.*;
import java.util.*;

/**
 *
 * @author  jd
 */
public class LoadTask implements Task {
    
    private String clientService;
    private String uri;
    private String routeKey;
    private byte[] message;
    private byte[] result;
    private int id;
    private long started;
    private String type;
    private Map properties;
    
    /** Creates a new instance of HTTPTask */
    public LoadTask(String clientService, byte[] message,int id,String type,Map properties) {
        this.id = id;
        this.clientService = clientService;
        this.message = message;
        this.type = type;
        this.properties = properties;
    }
    
    public void run() {        
        try {
            
            started = new Date().getTime();

            
            ClientRequest request = new ClientRequest();
            request.setMessage(message);
            request.setProperties(properties);
            
            AbstractClientService cs = null;
            cs = (com.webdeninteractive.xbotts.client.api.client.AbstractClientService) Class.forName(clientService).newInstance();        
            //run the client service            
            
            //TransactionContext tcontext = new TransactionContext( );
            //tcontext.setProcessId(id + "");
            //tcontext.setProcessName("load");
            //tcontext.setRequestMessage(request);
                      
            //cs.handle(tcontext);            
            //ClientResponse cr = (ClientResponse)tcontext.getResponseMessage();            
            
            ClientResponse cr = cs.runService(request);
            
            
            result = cr.getMessage();    
        }catch (Exception e) {
            Load.report(id,e,type);
            return;
        }
        Load.report(id, new Date().getTime() - started,result,type);        
    }
    
    /** Called by ThreadPoolManager when the task has been completed.
     * @param taskID The task ID that has been completed
     */
    public void taskCompleted(int taskID) {        
    }
    
    /** Called by the ThreadPoolManager when a task has been denied.
     */
    public void taskDenied() {
        //Load.report(id,new Exception("task denied"),type);
    }
    
}
